﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PyramidShadow
{
    class Block : LevelObject
    {
        enum Axis { X , Y }
        private SpawnPoint spawnPoint;
        public SpawnPoint SpawnPoint
        {
            get { return spawnPoint; }
        }

        public Block(Texture2D texture, Vector2 position, Level level) : base(texture,position,level)
        {
            spawnPoint = new SpawnPoint(position);
        }
        
        public override void Update(GameTime gameTime, KeyboardState currentKeyboardState)
        {
            if(!level.kollision.intersection(level.body.boundingBox,boundingBox))
                return;
            
            if(currentKeyboardState.IsKeyDown(Keys.Up))
                impact(Impact.Bottom,level.playerMoveSpeed);
            if (currentKeyboardState.IsKeyDown(Keys.Down))
                impact(Impact.Roof, level.playerMoveSpeed);
            if (currentKeyboardState.IsKeyDown(Keys.Left))
                impact(Impact.Right, level.playerMoveSpeed);
            if (currentKeyboardState.IsKeyDown(Keys.Right))
                impact(Impact.Left, level.playerMoveSpeed);
        }

        private void impact( Impact spotOfImpact, int speed)
        {
            switch (spotOfImpact)
            {
                case Impact.None: return;
                case Impact.Bottom: moveObject(Axis.Y, speed);
                                    break;
                case Impact.Left: moveObject(Axis.X, speed);
                                    break;
                case Impact.Right: moveObject(Axis.X, -speed);
                                    break;
                case Impact.Roof: moveObject(Axis.Y, -speed);
                                    break;
                default: return;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, boundingBox, Color.White);
        }

        private void moveObject(Axis axis, int speed)
        {
            if (axis == Axis.X)
            {
                boundingBox.X += speed;
                if (level.kollision.collide(boundingBox, BoxType.Object))
                {
                    boundingBox.X -= speed;
                    level.body.boundingBox.X -= speed;
                }
            }
            if (axis == Axis.Y)
            {
                boundingBox.Y += speed;
                if(level.kollision.collide(boundingBox,BoxType.Object))
                {
                   boundingBox.Y -= speed;
                   level.body.boundingBox.Y -= speed;
                }
            }
        }

        public void reset()
        {
            boundingBox.X = (int)spawnPoint.SpawnPosition.X;
            boundingBox.Y = (int)spawnPoint.SpawnPosition.Y;
        }
    }
}
